![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
The p-some npm package allows you to wait for a specified number of promises to be fulfilled. It is useful when you need only a certain number of promises to resolve successfully, and you don't care about the rest.
Wait for a specified number of promises to fulfill
This feature allows you to wait for a specified number of promises to be fulfilled. In this example, we have an array of promises, and we use p-some to wait for any 2 of them to resolve successfully. The result will be an array of the first 2 resolved values.
const pSome = require('p-some');
const promises = [
Promise.resolve(1),
Promise.resolve(2),
Promise.reject(new Error('error')),
Promise.resolve(3)
];
pSome(promises, { count: 2 }).then(values => {
console.log(values); // [1, 2]
});
Handle rejected promises
This feature demonstrates how p-some handles rejected promises. In this example, we have an array of promises with some of them being rejected. p-some will still wait for the specified number of promises to be fulfilled and will return the resolved values. If it cannot fulfill the count due to too many rejections, it will throw an error.
const pSome = require('p-some');
const promises = [
Promise.resolve(1),
Promise.reject(new Error('error1')),
Promise.reject(new Error('error2')),
Promise.resolve(2)
];
pSome(promises, { count: 2 }).then(values => {
console.log(values); // [1, 2]
}).catch(error => {
console.error(error);
});
The p-any package waits for any promise to be fulfilled. It is similar to p-some but instead of waiting for a specified number of promises, it resolves as soon as any one of the promises resolves. This can be useful when you only need the first successful result.
The p-all package runs multiple promise-returning & async functions with optional concurrency control. It is different from p-some as it waits for all promises to be fulfilled, rather than a specified number. This is useful when you need to ensure all promises are resolved before proceeding.
The promise-settle package waits for all promises to settle (either fulfilled or rejected) and returns an array of their results. Unlike p-some, it does not stop at a specified number of fulfilled promises but instead provides the outcome of all promises. This is useful for getting a complete picture of all promise results.
Wait for a specified number of promises to be fulfilled
Useful when you need the fastest of multiple promises.
$ npm install p-some
Checks 4 websites and logs the 2 fastest.
const got = require('got');
const pSome = require('p-some');
(async () => {
const input = [
got.head('github.com').then(() => 'github'),
got.head('google.com').then(() => 'google'),
got.head('twitter.com').then(() => 'twitter'),
got.head('medium.com').then(() => 'medium')
];
const [first, second] = await pSome(input, {count: 2});
console.log(first, second);
//=> 'google twitter'
})();
Returns a cancelable Promise
that is fulfilled when count
promises from input
are fulfilled. The fulfilled value is an Array
of the values from the input
promises in the order they were fulfilled. If it becomes impossible to satisfy count
, for example, too many promises rejected, it will reject with an AggregateError
error.
If you pass in cancelable promises, specifically promises with a .cancel()
method, that method will be called for the promises that are still unfulfilled when the returned Promise
is either fulfilled or rejected.
Type: Iterable<Promise | unknown>
An Iterable
collection of promises/values to wait for.
Type: Object
Required
Type: number
Minimum: 1
Number of promises from input
that have to be fulfilled until the returned promise is fulfilled.
Type: Function
Receives the value resolved by the promise. Used to filter out values that doesn't satisfy a condition.
Exposed for instance checking.
MIT © Sindre Sorhus
FAQs
Wait for a specified number of promises to be fulfilled
The npm package p-some receives a total of 223,061 weekly downloads. As such, p-some popularity was classified as popular.
We found that p-some demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.